package day11.handler.simpleChannelHandler.response;

import day11.packet.response.GroupMessageResponsePacket;
import day11.util.session.Session;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author chenshijie
 * @date 2024/1/3 10:39
 */

public class GroupMessageResponseHandler extends SimpleChannelInboundHandler<GroupMessageResponsePacket> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMessageResponsePacket groupMessageResponsePacket) throws Exception {
        if (groupMessageResponsePacket.isSuccess()) {
            Session fromUser = groupMessageResponsePacket.getFromUser();
            if (groupMessageResponsePacket.getJoinGroupStatus() == GroupMessageResponsePacket.JOIN_GROUP_STATUS) {
                System.out.println("┌───────────────────────────────────────");
                System.out.println("│收到了群【"+ groupMessageResponsePacket.getGroupId() +"】的消息");
                System.out.println("│(" + fromUser.getUserId() + ")" + fromUser.getUserName() + "加入了该群。");
                System.out.println("└───────────────────────────────────────");
            } else {
                System.out.println("┌───────────────────────────────────────");
                System.out.println("│收到了群【"+ groupMessageResponsePacket.getGroupId() +"】的消息");

                System.out.println("│(" + fromUser.getUserId() + ")" + fromUser.getUserName() + ": " + groupMessageResponsePacket.getMessage());
                System.out.println("└───────────────────────────────────────");
            }

        } else {
            System.err.println(groupMessageResponsePacket.getMessage());
        }
    }
}
